﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Receipt.aspx.cs" Inherits="Main_ServiceInvoice_Receipt"
    Culture="en-GB" %>

<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Service Invoice Receipt</title>
    <link href="../../Styles/accounts.css" rel="stylesheet" type="text/css" />

    <script src="../../JavaScript/Commons.js" type="text/javascript"></script>

    <script type="text/javascript">
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        function GetLastNumber(sText) {
            var Char;
            var lastRow = 0;
            for (i = 0; i < sText.length; i++) {
                Char = sText.charAt(i);
                if (!isNaN(parseFloat(Char))) {
                    lastRow = sText.toString().substr(i);
                    break;
                }
            }
            return lastRow;
        }
        function check(id) {
            //alert(document.getElementById(id).value);
            if (document.getElementById(id).value == 'rdldd') {
                document.getElementById("lblcashremit").innerHTML = 'Remitting Bank';
                document.getElementById("ddlCashType").style.display = "none";
                document.getElementById("ddlBankType").style.display = "inline";

                document.getElementById("tddd").style.display = "inline";
                document.getElementById("tdddtext").style.display = "inline";
                document.getElementById("tddate").style.display = "inline";
                document.getElementById("tddatetext").style.display = "inline";
                document.getElementById("tdbank").style.display = "inline";
                document.getElementById("tdbanktext").style.display = "inline";

                document.getElementById("txtdd").disabled = false;
                document.getElementById("txtdd").className = "inputnumber";
                document.getElementById("txtBranch").disabled = false;
                document.getElementById("txtBranch").className = "input";
                document.getElementById("txtmodedate").disabled = false;
                document.getElementById("txtmodedate").className = "input";
                document.getElementById("ImageButton1").disabled = false;
                document.getElementById("ImageButton1").className = "inputnumber";

                document.getElementById("tdcard").style.display = "none";
                document.getElementById("tdcardtext").style.display = "none";
                document.getElementById("tdissued").style.display = "none";
                document.getElementById("tdissuedtext").style.display = "none";
            }
            else if (document.getElementById(id).value == 'rdlcash') {
                document.getElementById("lblcashremit").innerHTML = 'Cash Ledger';
                document.getElementById("ddlCashType").style.display = "inline";
                document.getElementById("ddlBankType").style.display = "none";

                document.getElementById("tddd").style.display = "inline";
                document.getElementById("tdddtext").style.display = "inline";
                document.getElementById("tddate").style.display = "inline";
                document.getElementById("tddatetext").style.display = "inline";
                document.getElementById("tdbank").style.display = "inline";
                document.getElementById("tdbanktext").style.display = "inline";

                document.getElementById("txtdd").disabled = true;
                document.getElementById("txtdd").value = '';
                document.getElementById("txtdd").className = "DisableInputNumber";
                document.getElementById("txtBranch").value = '';
                document.getElementById("txtBranch").disabled = true;
                document.getElementById("txtBranch").className = "DisableInput";
                document.getElementById("txtmodedate").disabled = true;
                document.getElementById("txtmodedate").className = "DisableInput";
                document.getElementById("txtmodedate").value = '';
                document.getElementById("ImageButton1").disabled = true;
                document.getElementById("ImageButton1").className = "DisableInputNumber";

                document.getElementById("tdcard").style.display = "none";
                document.getElementById("tdcardtext").style.display = "none";
                document.getElementById("tdissued").style.display = "none";
                document.getElementById("tdissuedtext").style.display = "none";
            }
            else if (document.getElementById(id).value == 'rdlcard') {
                document.getElementById("lblcashremit").innerHTML = 'Remitting Bank';
                document.getElementById("ddlCashType").style.display = "none";
                document.getElementById("ddlBankType").style.display = "inline";

                document.getElementById("tdcard").style.display = "inline";
                document.getElementById("tdcardtext").style.display = "inline";
                document.getElementById("tdissued").style.display = "inline";
                document.getElementById("tdissuedtext").style.display = "inline";

                document.getElementById("tddd").style.display = "none";
            //    document.getElementById("tddd").value = '';
                document.getElementById("tdddtext").style.display = "none";
                document.getElementById("tddate").style.display = "none";
                document.getElementById("tddatetext").style.display = "none";
                document.getElementById("tdbank").style.display = "none";
                document.getElementById("tdbanktext").style.display = "none";
            }
        }
        function GetLastNumber(sText) {
            var Char;
            var lastRow = 0;
            for (i = 0; i < sText.length; i++) {
                Char = sText.charAt(i);
                if (!isNaN(parseFloat(Char))) {
                    lastRow = sText.toString().substr(i);
                    break;
                }
            }
            return lastRow;
        }

        function ValidateAddMoreMain() {
            var flag = true;
            var lastNo = document.getElementById("hidRowCount").value;
            for (var i = 1; i <= lastNo; i++) {
                if (document.getElementById("ddlInvoiceParticular" + i).value == "0") {
                    alert("Specify Particulars");
                    flag = false;
                    document.getElementById("ddlInvoiceParticular" + i).focus();
                    break;
                }
                else if (document.getElementById("ddlServices" + i).value == "0") {
                    alert("Specify Services");
                    flag = false;
                    document.getElementById("ddlServices" + i).focus();
                    break;
                }
                else if (document.getElementById("txtTotalAmount" + i).value == "" || document.getElementById("txtTotalAmount" + i).value == "0" || document.getElementById("txtTotalAmount" + i).value == "0.00") {
                    alert("Specify credit note amount");
                    flag = false;
                    document.getElementById("txtTotalAmount" + i).focus();
                    break;
                }
            }
            return flag;
        }

        function NoParticularRepeat(id, type) {
            if (CheckTypeParticular(type, id)) {
                var flag = true;
                var currentControl;
                var currentValue;
                var previousControl;
                var previousValue;

                if (type == "txt") {
                    currentControl = document.getElementById(id);
                    currentValue = document.getElementById(id).value;
                }
                else if (type == "ddl") {
                    currentControl = document.getElementById(id);
                    currentValue = currentControl.options[currentControl.selectedIndex].value;
                }
                var count = document.getElementById("hidRowCount").value;
                for (var i = 1; i <= count; i++) {
                    var ddlType = document.getElementById("ddlType" + i.toString());
                    if (ddlType.value == "1" || ddlType.value == "3" || ddlType.value == "4" || ddlType.value == "0") {
                        previousControl = document.getElementById("txtParticular" + i.toString());
                        previousValue = document.getElementById("txtParticular" + i.toString()).value;
                    }
                    else if (ddlType.value == "2") {
                        previousControl = document.getElementById("ddlParticular" + i.toString());
                        previousValue = document.getElementById("ddlParticular" + i.toString()).value;
                    }
                    if (currentControl != previousControl) {
                        if (currentValue != "" && previousValue != "") {
                            if (currentValue == previousValue) {
                                alert("Please select unique particulars !");
                                if (type == "txt") {
                                    document.getElementById(id).value = "";
                                }
                                else if (type == "ddl") {
                                    currentControl.selectedIndex = 0;
                                }
                                currentControl.focus();
                                flag = false;
                                break;
                            }
                        }
                    }
                }
                if (flag == true && type == "ddl") {
                    setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
                }
                return flag;
            }

        }
        function CheckFormatCustomerRecvAmt() {
            var flag = true;
            var ddlFormat = document.getElementById("ddlFormat");
            var ddlCustomer = document.getElementById("ddlCustomer");
            var txtRecievedAmt = document.getElementById("txtRecievedAmt").value;
            var txtDate = document.getElementById("txtDate").value;
            if (ddlFormat.options[ddlFormat.selectedIndex].value == "--- Select Format ---" || ddlFormat.selectedIndex == 0) {
                alert("Please select format !");
                document.getElementById("ddlFormat").focus();
                flag = false;
            }
            else if (ddlCustomer.options[ddlCustomer.selectedIndex].value == "-- Select Customer --" || ddlCustomer.selectedIndex == 0) {
                alert("Please select Customer !");
                document.getElementById("ddlCustomer").focus();
                flag = false;
            }
            else if (txtDate == "" || txtDate == "__/__/____") {
                alert("Please select date !");
                document.getElementById("txtDate").focus();
                flag = false;
            }
            else if (txtRecievedAmt == "" || parseFloat(txtRecievedAmt) <= 0) {
                alert("Please enter Received amount !");
                document.getElementById("txtRecievedAmt").focus();
                flag = false;
            }
            return flag;
        }
        function CheckTypeParticular(type, id) {
            var flag = true;
            var Char;
            var total = 0;
            for (p = 0; p < id.toString().length; p++) {
                Char = id.toString().charAt(p);
                if (!isNaN(parseFloat(Char))) {
                    Char = id.toString().substr(p, id.toString().length);
                    break;
                }
            }
            var ddlType = document.getElementById("ddlType" + Char.toString());
            if (ddlType.options[ddlType.selectedIndex].value == "0") {
                alert("Please select type !");
                if (type = "txt") {
                    document.getElementById("txtParticular" + Char.toString()).value = "";
                }
                else if (type = "ddl") {
                    document.getElementById("ddlParticular" + Char.toString()).value = "0";
                }
                document.getElementById("ddlType" + Char.toString()).focus();
                flag = false;
            }
            else if (ddlType.value == "1" || ddlType.value == "4" || ddlType.value == "0") {
                var txtParticular = document.getElementById("txtParticular" + Char.toString()).value;
                if (txtParticular == "") {
                    alert("Please enter particular !!");
                    document.getElementById("txtParticular" + Char.toString()).focus();
                    flag = false;
                }
            }
            else if (ddlType.value == "2" || ddlType.selectedIndex == 2) {
                ddlParticulr = document.getElementById("ddlParticular" + Char.toString());
                ddlParticulrVal = ddlParticulr.options[ddlParticulr.selectedIndex].value;
                if (ddlParticulrVal == "" || ddlParticulr.selectedIndex == 0) {
                    alert("Please select particular !!");
                    document.getElementById("ddlParticular" + Char.toString()).focus();
                    flag = false;
                }
            }

            return flag;
        }
        function CheckWriteoffTDS(type, id) {
            var flag = true;
            var Char;
            var total = 0;
            for (p = 0; p < id.toString().length; p++) {
                Char = id.toString().charAt(p);
                if (!isNaN(parseFloat(Char))) {
                    Char = id.toString().substr(p, id.toString().length);
                    break;
                }
            }
            var count = document.getElementById("hidRowCount").value;
            var txtDetailsBalanceAmt = document.getElementById("txtDetailsBalanceAmt" + Char).value;
            var txtDetailsTDSAmt = document.getElementById("txtDetailsTDSAmt" + Char).value;
            var txtDetailsWriteOffAmt = document.getElementById("txtDetailsWriteOffAmt" + Char).value;
            var checkcond = 0;
            if (type == "writeoff") {
                if (txtDetailsTDSAmt != "") {
                    total = parseFloat(txtDetailsBalanceAmt) - parseFloat(txtDetailsTDSAmt);
                    checkcond = (parseFloat(txtDetailsWriteOffAmt) + parseFloat(txtDetailsTDSAmt)).toFixed(2);
                }
                else {
                    checkcond = parseFloat(txtDetailsWriteOffAmt).toFixed(2);
                }
                if (parseFloat(checkcond) > parseFloat(txtDetailsBalanceAmt)) {
                    if (txtDetailsTDSAmt == "") {
                        alert("Writeoff can not be greater than " + parseFloat(txtDetailsBalanceAmt).toFixed(2));
                        flag = false;
                    }
                    else {
                        alert("Writeoff can not be greater than " + total.toFixed(2));
                        flag = false;
                    }
                    document.getElementById("txtDetailsWriteOffAmt" + Char).value = "0";
                    document.getElementById("txtDetailsWriteOffAmt" + Char).focus();
                }
            }
            else if (type == "tds") {
                if (txtDetailsWriteOffAmt != "") {
                    total = parseFloat(txtDetailsBalanceAmt) - parseFloat(txtDetailsWriteOffAmt);
                    checkcond = (parseFloat(txtDetailsWriteOffAmt) + parseFloat(txtDetailsTDSAmt)).toFixed(2);
                }
                else {
                    checkcond = parseFloat(txtDetailsTDSAmt).toFixed(2);
                }
                if (parseFloat(checkcond) > parseFloat(txtDetailsBalanceAmt)) {
                    if (txtDetailsWriteOffAmt == "") {
                        alert("TDS can not be greater than " + parseFloat(txtDetailsBalanceAmt).toFixed(2));
                        flag = false;
                    }
                    else {
                        alert("TDS can not be greater than " + total.toFixed(2));
                        flag = false;
                    }
                    document.getElementById("txtDetailsTDSAmt" + Char).value = "0";
                    document.getElementById("txtDetailsTDSAmt" + Char).focus();
                }
            }
            return flag;
        }
        function ChkReceivedPendngTtlAmt(type) {
            var flag = true;
            var finaltotal = 0;
            var leftrecived = 0;
            var finaltotalForAlert = 0;
            var FinalTotalToCheck = 0;
            var received = document.getElementById("txtRecievedAmt").value;
            var count = document.getElementById("hidRowCount").value;
            if (count == 1) {
                var amount = document.getElementById("txtTotalAmt1").value;
                var ddlType = document.getElementById("ddlType1");
                var hidpending = document.getElementById("hidPendingAmount1").value;
                if (amount != "") {
                    if (parseFloat(amount) > parseFloat(received)) {
                        if (type == "ttlAmt") {
                            alert("Amount can not be greater than " + received);
                            document.getElementById("txtTotalAmt1").focus();
                        }
                        else if (type == "received") {
                            document.getElementById("txtTotalAmt1").value = 0;
                        }
                        flag = false;
                    }
                    else {
                        if (parseFloat(amount) > parseFloat(hidpending)) {
                            if (type == "ttlAmt") {
                                if (ddlType.value == "2" || ddlType.selectedIndex == "2") {
                                    alert("Amount can not be greater than " + hidpending);
                                    //document.getElementById("txtTotalAmt1").focus();
                                    document.getElementById("txtTotalAmt1").value = hidpending;
                                    document.getElementById("txtDetailsBalanceAmt1").value = "0";
                                    flag = false;
                                }
                            }
                        }
                        else {
                            if (ddlType.value == "2" || ddlType.selectedIndex == "2") {
                                document.getElementById("txtDetailsBalanceAmt1").value = parseFloat(parseFloat(hidpending) - parseFloat(amount)).toFixed(2);
                            }
                        }
                    }
                }
            }
            else {
                for (var i = 1; i <= count; i++) {
                
                    var ddlType = document.getElementById("ddlType" + i.toString());
                    var amount = document.getElementById("txtTotalAmt" + i.toString()).value;
                    var hidpending = document.getElementById("hidPendingAmount" + i.toString()).value;
                    if (amount != "") {
                   
                        finaltotalForAlert = parseFloat(finaltotal);
                        finaltotal = parseFloat(finaltotal) + parseFloat(amount);
                        if (i < count) {
                            FinalTotalToCheck = parseFloat(finaltotal);
                        }
                        leftrecived =parseFloat(received)-parseFloat(finaltotalForAlert) ;
                        if (parseFloat(amount) > Math.abs(leftrecived)) {
                          
                            if (type == "ttlAmt") {
                                if (Math.abs(finaltotal) > parseFloat(received)) {
                                    alert("Amount can not be greater than " + leftrecived);
                                    document.getElementById("txtTotalAmt" + i.toString()).focus();
                                }
                                else if (Math.abs(amount) > parseFloat(received)) {
                                alert("Amount can not be greater than " + received.toString());
                                    document.getElementById("txtTotalAmt" + i.toString()).focus();
                                }
                            }
                            else if (type == "received") {
                                document.getElementById("txtTotalAmt" + i.toString()).value = "0";
                            }
                            flag = false;
                            break;
                        }
                        else {
                          
                            if (parseFloat(amount) > parseFloat(hidpending)) {
                                if (type == "ttlAmt") {
                                    if (ddlType.value == "2" || ddlType.selectedIndex == 2) {
                                        alert("Amount can not be greater than " + hidpending);
                                        //document.getElementById("txtTotalAmt"+i.toString()).focus();
                                        document.getElementById("txtTotalAmt" + i.toString()).value = hidpending;
                                        document.getElementById("txtDetailsBalanceAmt" + i.toString()).value = "0";
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                            else {
                                if (ddlType.value == "2" || ddlType.selectedIndex == "2") {
                                    document.getElementById("txtDetailsBalanceAmt" + i.toString()).value = parseFloat((parseFloat(hidpending) - parseFloat(amount)).toFixed(2));
                                }
                            }
                        }
                    }
                }
            }
            return flag;
        }
        function CheckTypeParticularervice(id) {
            var flag = true;
            var Char;
            for (p = 0; p < id.toString().length; p++) {
                Char = id.toString().charAt(p);
                if (!isNaN(parseFloat(Char))) {
                    Char = id.toString().substr(p, id.toString().length);
                    break;
                }
            }
            var f = document.forms[0];
            var rbtAmountType = f.elements["rbtAmountType"];

            var ddlServ = document.getElementById("ddlNature" + Char);
            var ddlType = document.getElementById("ddlType" + Char);
            if (ddlType.options[ddlType.selectedIndex].value == "0") {
                alert("Please select type !");
                document.getElementById("ddlType" + Char.toString()).focus();
                flag = false;
            }
            else if (ddlType.value == "1" || ddlType.value == "4" || ddlType.value == "0") {
                var txtParticular = document.getElementById("txtParticular" + Char.toString()).value;
                if (txtParticular == "") {
                    alert("Please enter particular !!");
                    document.getElementById("txtParticular" + Char.toString()).focus();
                    flag = false;
                }
                else if (ddlServ.options[ddlServ.selectedIndex].value == "0") {
                    alert("Please select service !");
                    document.getElementById("ddlNature" + Char).focus();
                    flag = false;
                }
            }
            else if (rbtAmountType[0].checked && ddlType.value == "2") {
                ddlParticulr = document.getElementById("ddlParticular" + Char.toString());
                ddlParticulrVal = ddlParticulr.options[ddlParticulr.selectedIndex].value;
                if (ddlParticulrVal == "0" || ddlParticulr.selectedIndex == 0) {
                    alert("Please select particular !!");
                    document.getElementById("ddlParticular" + Char.toString()).focus();
                    flag = false;
                }
            }
            else if (rbtAmountType[1].checked) {
            if (ddlServ.options[ddlServ.selectedIndex].value == "0") {
                alert("Please select service !");
                document.getElementById("ddlNature" + Char).focus();
                flag = false;
            }
                else if (ddlType.value == "1" || ddlType.value == "4" || ddlType.value == "0") {
                    var txtParticular = document.getElementById("txtParticular" + Char.toString()).value;
                    if (txtParticular == "") {
                        alert("Please enter particular !!");
                        document.getElementById("txtParticular" + Char.toString()).focus();
                        flag = false;
                    }
                }
                else  if (ddlType.value == "2") {
                    ddlParticulr = document.getElementById("ddlParticular" + Char.toString());
                    ddlParticulrVal = ddlParticulr.options[ddlParticulr.selectedIndex].value;
                    if (ddlParticulrVal == "0" || ddlParticulr.selectedIndex == 0) {
                        alert("Please select particular !!");
                        document.getElementById("ddlParticular" + Char.toString()).focus();
                        flag = false;
                    }
                }
            }
            return flag;
        }

        function NoServiceRepeat(id) {
            if (CheckTypeParticular('ddlS', id)) {
                var f = document.forms[0];
                var rbtAmountType = f.elements["rbtAmountType"];
                var currentParticular;
                var currentParticularValue;

                if (rbtAmountType[1].checked) {
                    var flag = true;
                    var ddl = document.getElementById(id);
                    var currentvalue = ddl.options[ddl.selectedIndex].value;
                    var strlength = ddl.id.toString().length;
                    var index = parseInt(ddl.id.substring(9, strlength), strlength);
                    var ddlType = document.getElementById("ddlType" + index.toString());

                    if (ddlType.value == "1" || ddlType.value == "4") {
                        currentParticular = document.getElementById("txtParticular" + index.toString());
                        currentParticularValue = document.getElementById("txtParticular" + index.toString()).value;
                    }
                    else if (ddlType.value == "2") {
                        currentParticular = document.getElementById("ddlParticular" + index.toString());
                        currentParticularValue = currentParticular.options[currentParticular.selectedIndex].value;
                    }

                    var count = document.getElementById('hidRowCount').value;
                    for (var i = 1; i <= count; i++) {

                        if (ddlType.value == "1" || ddlType.value == "4") {
                            previousParticular = document.getElementById("txtParticular" + index.toString());
                            previousParticularValue = document.getElementById("txtParticular" + index.toString()).value;
                        }
                        else if (ddlType.value == "2") {
                            previousParticular = document.getElementById("ddlParticular" + i.toString());
                            previousParticularValue = currentParticular.options[currentParticular.selectedIndex].value;
                        }

                        var previous = document.getElementById("ddlNature" + i.toString());
                        var previousvalue = previous.options[previous.selectedIndex].value;
                        if (currentParticular != previousParticular) {
                            if (currentParticularValue == previousParticularValue && currentParticularValue != '0') {
                                if (ddl != previous) {
                                    if (currentvalue == previousvalue && currentvalue != '0') {
                                        alert('Please select unique service !');
                                        ddl.selectedIndex = 0;
                                        ddl.focus();
                                        flag = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (flag == true) {
                    setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
                }
                return flag;
            }
        }

        function CheckTableMain_Final() {
            var flag = true;
            var totalAmount = 0;
            var receivedAmt = document.getElementById("txtRecievedAmt").value;
            var f = document.forms[0];
            var rbtAmountType = f.elements["rbtAmountType"];
            var Count = document.getElementById("hidRowCount").value;

            for (var Char = 1; Char <= Count; Char++) {
                var ddlServ = document.getElementById("ddlNature" + Char);
                var ddlType = document.getElementById("ddlType" + Char);
                var txtTotalAmt = document.getElementById("txtTotalAmt" + Char).value;
                totalAmount = parseFloat(totalAmount) + parseFloat(txtTotalAmt);
                if (ddlType.options[ddlType.selectedIndex].value == "0") {
                    alert("Please select type !");
                    document.getElementById("ddlType" + Char.toString()).focus();
                    flag = false;
                    break;
                }
                else if (ddlType.value == "1" || ddlType.value == "4" || ddlType.value == "0") {
                    var txtParticular = document.getElementById("txtParticular" + Char.toString()).value;
                    if (txtParticular == "") {
                        alert("Please enter particular !!");
                        document.getElementById("txtParticular" + Char.toString()).focus();
                        flag = false;
                        break;
                    }
                    else if (ddlServ.options[ddlServ.selectedIndex].value == "0") {
                        alert("Please select service !");
                        document.getElementById("ddlNature" + Char).focus();
                        flag = false;
                        break;
                    }
                    else if (txtTotalAmt == "" || parseFloat(txtTotalAmt) <= 0) {
                        alert("Please enter amount !");
                        document.getElementById("txtTotalAmt" + Char).focus();
                        flag = false;
                        break;
                    }
                }
                else if (rbtAmountType[0].checked && ddlType.value == "2") {
                    ddlParticulr = document.getElementById("ddlParticular" + Char.toString());
                    ddlParticulrVal = ddlParticulr.options[ddlParticulr.selectedIndex].value;
                    if (ddlParticulrVal == "0" || ddlParticulr.selectedIndex == 0) {
                        alert("Please select particular !!");
                        document.getElementById("ddlParticular" + Char.toString()).focus();
                        flag = false;
                        break;
                    }
                    else if (txtTotalAmt == "" || parseFloat(txtTotalAmt) <= 0) {
                        alert("Please enter amount !");
                        document.getElementById("txtTotalAmt" + Char).focus();
                        flag = false;
                        break;
                    }
                }

                else if (rbtAmountType[1].checked) {
                    if (ddlServ.options[ddlServ.selectedIndex].value == "0") {
                        alert("Please select service !");
                        document.getElementById("ddlNature" + Char).focus();
                        flag = false;
                        break;
                    }
                    else if (ddlType.value == "1" || ddlType.value == "4" || ddlType.value == "0") {
                        var txtParticular = document.getElementById("txtParticular" + Char.toString()).value;
                        if (txtParticular == "") {
                            alert("Please enter particular !!");
                            document.getElementById("txtParticular" + Char.toString()).focus();
                            flag = false;
                            break;
                        }
                    }
                    else if (ddlType.value == "2") {
                        ddlParticulr = document.getElementById("ddlParticular" + Char.toString());
                        ddlParticulrVal = ddlParticulr.options[ddlParticulr.selectedIndex].value;
                        if (ddlParticulrVal == "0" || ddlParticulr.selectedIndex == 0) {
                            alert("Please select particular !!");
                            document.getElementById("ddlParticular" + Char.toString()).focus();
                            flag = false;
                            break;
                        }
                    }
                    else if (txtTotalAmt == "" || parseFloat(txtTotalAmt) <= 0) {
                        alert("Please enter amount !");
                        document.getElementById("txtTotalAmt" + Char).focus();
                        flag = false;
                        break;
                    }
                }
                else if (parseFloat(totalAmount) != parseFloat(receivedAmt)) {
                    if (parseFloat(totalAmount) > parseFloat(receivedAmt)) {
                        alert("Total Amount can not be greater than received amount !");
                        document.getElementById("txtTotalAmt" + Char).focus();
                        flag = false;
                        break;
                    }
                    else if (parseFloat(totalAmount) > parseFloat(receivedAmt)) {
                        alert("Total Amount can not be less than received amount !");
                        document.getElementById("txtTotalAmt" + Char).focus();
                        flag = false;
                        break;
                    }
                }
            }
            return flag;
        }

        function CheckBeforeSave() {
            var flag = true;
            if (CheckFormatCustomerRecvAmt() == true) {
                if (CheckTableMain_Final() == true) {
                    var receiptNo = document.getElementById("txtReceiptNo").value;
                    var remittingBank = document.getElementById("ddlBankType");
                    var remittingCash = document.getElementById("ddlCashType");
                    var remittingBankChecked = remittingBank.options[remittingBank.selectedIndex].value;
                    var remittingCashChecked = remittingCash.options[remittingCash.selectedIndex].value;
                    if (receiptNo == "" || receiptNo == "Number") {
                        alert("Please enter receipt no !!");
                        document.getElementById("txtReceiptNo").focus();
                        flag = false;
                    }
                    else {
                      
                        var f = document.forms[0];
                        var rdbtnBank = f.elements["rdldd"];
                        var rdbtnCash = f.elements["rdlcash"];
                        var rdbtnCredit = f.elements["rdlcard"];
                        if (rdbtnBank.checked == true) {
                            if (remittingBankChecked == "0" || remittingBank.selectedIndex == 0) {
                                alert("Please select remitting bank !!");
                                document.getElementById("ddlBankType").focus();
                                flag = false;
                            }
                        }
                        else if (rdbtnCash.checked == true) {
                            if (remittingCashChecked == "0" || remittingCash.selectedIndex == 0) {
                                alert("Please select cash ledger !!");
                                document.getElementById("ddlCashType").focus();
                                flag = false;
                            }
                        }
                        else if (rdbtnCredit.checked == true) {
                            if (remittingBankChecked == "0" || remittingBank.selectedIndex == 0) {
                                alert("Please select remitting bank !!");
                                document.getElementById("ddlBankType").focus();
                                flag = false;
                            }
                        }
                    }
                }
                else {
                    flag = false;
                }
            }
            else {
                flag = false;
            }
            return flag;
        }    
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div class="divmain" style="width: 96%; margin-left: 1%;">
        <acc:heading ID="pagehead" runat="server" HeadingText="Receipt" />
        <div style="margin-left: 5%; margin-right: 3%; width: 85%" align="center">
            <asp:Label ID="lblMsg" runat="server" Font-Bold="true"></asp:Label>
            <asp:HiddenField ID="hidTDSLID" runat="server" Value="0" />
            <asp:HiddenField id="hidWriteoffLID" runat="server" Value="0" />
        </div>
    </div>
    <div style="width: 98%; margin-top: 10px; margin-left: 1%;">
        <table style="width: 100%; margin-right: 5px;" cellpadding="2" class="tablecontent">
            <tr>
                <td>
                    <b>Format</b><span style="color: Red"> *</span>
                </td>
                <td>
                    <asp:DropDownList ID="ddlFormat" runat="server" onblur="return CheckFormatCustomerRecvAmt();"
                        CssClass="dropdown" OnSelectedIndexChanged="ddlFormat_SelectedIndex" AutoPostBack="true">
                    </asp:DropDownList>
                </td>
                <td>
                    <b>Customer <span style="color: Red;">*</span></b>
                </td>
                <td>
                    <asp:DropDownList ID="ddlCustomer" runat="server" onblur="return CheckFormatCustomerRecvAmt();"
                        CssClass="dropdown" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomer_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <b>Receipt No.</b> <span style="color: Red">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtPrefix" runat="server" Width="60px" CssClass="input" onKeyPress="return false"
                        onKeydown="return false"></asp:TextBox>
                    <ajaxcontrol:textboxwatermarkextender id="prefixWater" runat="server" targetcontrolid="txtPrefix"
                        watermarktext="Prefix" watermarkcssclass="watermark_textbox">
                    </ajaxcontrol:textboxwatermarkextender>
                    <asp:TextBox ID="txtReceiptNo" runat="server" Width="60px" CssClass="inputnumber"
                        ValidationGroup="V" MaxLength="10"></asp:TextBox>
                    <ajaxcontrol:textboxwatermarkextender id="voucherWater" runat="server" targetcontrolid="txtReceiptNo"
                        watermarktext="Number" watermarkcssclass="watermark_textbox">
                    </ajaxcontrol:textboxwatermarkextender>
                    <asp:TextBox ID="txtSuffix" runat="server" Width="60px" CssClass="input" onKeyPress="return false"
                        onKeydown="return false"></asp:TextBox>
                    <ajaxcontrol:textboxwatermarkextender id="suffixWater" runat="server" targetcontrolid="txtSuffix"
                        watermarktext="Suffix" watermarkcssclass="watermark_textbox">
                    </ajaxcontrol:textboxwatermarkextender>
                </td>
                <td>
                    <b>Balance</b>
                </td>
                <td>
                    <asp:TextBox ID="txtBalance" runat="server" CssClass="DisableInputNumber" onKeyDown="return EnabledTabKey(this.id, event);"></asp:TextBox>
                </td>
                <td>
                    <b>Amount Received <span style="color: Red;">* </span></b>
                </td>
                <td>
                    <asp:TextBox ID="txtRecievedAmt" runat="server" CssClass="inputnumber" onblur="return CheckFormatCustomerRecvAmt();ChkReceivedPendngTtlAmt('received');"
                        OnKeyPress="return isNumberKey(this.ID,event);" Width="75px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <b>Date</b> <span style="color: Red">*</span>
                </td>
                <td>
                    <asp:TextBox ID="txtDate" runat="server" onblur="return CheckFormatCustomerRecvAmt();"
                        CssClass="input"></asp:TextBox>
                    <%--<asp:RequiredFieldValidator ID="reqDate" runat="server" ControlToValidate="txtDate" ValidationGroup="V" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>--%>&nbsp;
                    <asp:ImageButton ID="btnDate" runat="server" ImageUrl="~/Images/calendar.gif" />
                    <ajaxcontrol:calendarextender id="cal" runat="server" popupbuttonid="btnDate" targetcontrolid="txtDate"
                        format="dd/MM/yyyy">
                    </ajaxcontrol:calendarextender>
                    <ajaxcontrol:maskededitextender id="maskExt" runat="server" masktype="Date" mask="99/99/9999"
                        targetcontrolid="txtDate" culturename="pt-BR">
                    </ajaxcontrol:maskededitextender>
                    <ajaxcontrol:maskededitvalidator id="maskValid" runat="server" controlextender="maskExt"
                        invalidvaluemessage="Invalid Date" isvalidempty="true" display="Dynamic" validationgroup="V"
                        controltovalidate="txtDate"></ajaxcontrol:maskededitvalidator>
                </td>
                <td>
                    <b>Ref No.</b>
                </td>
                <td>
                    <asp:TextBox ID="txtRefno" runat="server" CssClass="inputnumber"></asp:TextBox>
                </td>
                <td colspan="2">
                    <asp:RadioButtonList ID="rbtAmountType" runat="server" 
                        RepeatDirection="Horizontal" AutoPostBack="true"
                        onselectedindexchanged="rbtAmountType_SelectedIndexChanged">
                        <asp:ListItem Text="Proportional" Value="0" Selected="True" style="padding-right: 15px;"></asp:ListItem>
                        <asp:ListItem Text="Actual" Value="1"></asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 98%; margin-top: 10px; margin-left: 1%;">
        <b>Bills/D.B Note/Adv Receipt Details </b>
        <br />
        <asp:HiddenField ID="hidRowCount" Value="1" runat="server" />
        <table id="tblRecieptDetails" runat="server" style="width: 100%;" cellpadding="2"
            cellspacing="0" class="tablecontent">
            <tr>
                <td class="headtd" style="border-right: 1px solid #818181; width: 15%;">
                    Type
                </td>
                <td class="headtd" style="border-right: 1px solid #818181; width: 20%;">
                    Particulars
                </td>
                <td class="headtd" style="border-right: 1px solid #818181; width: 20%;">
                    Nature of Service
                </td>
                <td class="headtd" style="border-right: 1px solid #818181; width: 15%;">
                    Original Amount
                </td>
                <td class="headtd" style="border-right: 1px solid #818181; width: 15%;">
                    Pending Amount
                </td>
                <td class="headtd" style="width: 15%;">
                    Amount
                </td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="ddlType1" runat="server" CssClass="dropdown" onclick="return CheckFormatCustomerRecvAmt();"
                        AutoPostBack="true" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" Width="98%">
                        <asp:ListItem Text="-- Select Type --" Value="0"></asp:ListItem>
                        <asp:ListItem Text="New Bill" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Towards Bill" Value="2"></asp:ListItem>
                        <asp:ListItem Text="On A/C" Value="3"></asp:ListItem>
                        <asp:ListItem Text="Advance" Value="4"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="txtParticular1" runat="server" onblur="return CheckTypeParticular('txt',this.id); NoParticularRepeat(this.id,'txt');"
                        CssClass="input" Width="98%"></asp:TextBox>
                    <asp:DropDownList ID="ddlParticular1" runat="server" CssClass="dropdown" onchange="return NoParticularRepeat(this.id,'ddl');"
                        OnSelectedIndexChanged="ddlParticulars_SelectedIndexChanged" AutoPostBack="true"
                        Width="98%">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:DropDownList ID="ddlNature1" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true"
                        onchange="return NoServiceRepeat(this.id);" OnSelectedIndexChanged="ddlNature_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:TextBox ID="txtOriginalAmt1" runat="server" CssClass="DisableInputNumber" onKeyDown="return EnabledTabKey(this.id, event);"
                        Width="98%"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="txtPendingAmt1" runat="server" CssClass="DisableInputNumber" onKeyDown="return EnabledTabKey(this.id, event);"
                        Width="98%"></asp:TextBox>
                    <asp:HiddenField ID="hidPendingAmount1" runat="server" Value="0" />
                </td>
                <td>
                    <asp:TextBox ID="txtTotalAmt1" runat="server" CssClass="inputnumber" onblur="return ChkReceivedPendngTtlAmt('ttlAmt');"
                        OnKeyDown="return CheckTypeParticularervice(this.id);" OnKeyPress="return isNumberKey(this.id,event);"
                        Width="98%"></asp:TextBox>
                </td>
            </tr>
        </table>
        <table style="margin-top: 5px">
            <tr>
                <td>
                    <asp:LinkButton ID="btnAddMore" runat="server" OnClick="ADDMore_Click"> 
                            <img src="../../Images/More.jpg" alt="Add New Row" />
                    </asp:LinkButton>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 98%; margin-top: 15px; margin-left: 1%;">
        <b>TDS/Writeoff Details</b>
        <asp:HiddenField ID="hidWriteOffCount" runat="server" Value="0" />
        <asp:HiddenField ID="hidTableRowIndex" runat="server" />
        <table style="width: 100%;" class="tablecontent" id="tblWriteOffDetails" runat="server">
            <tr>
                <td align="center" class="headtd" style="border-right: 1px solid #818181; border-bottom: 1px solid #818181;
                    width: 20%;">
                    <b>Particulars</b>
                </td>
                <td align="center" class="headtd" style="border-right: 1px solid #818181; border-bottom: 1px solid #818181;
                    width: 20%;">
                    <b>Natures of Service</b>
                </td>
                <td align="center" class="headtd" style="border-right: 1px solid #818181; border-bottom: 1px solid #818181;
                    width: 20%;">
                    <b>Balance Amount</b>
                </td>
                <td align="center" class="headtd" style="border-right: 1px solid #818181; border-bottom: 1px solid #818181;
                    width: 20%;">
                    <b>TDS</b>
                </td>
                <td align="center" class="headtd" style="border-bottom: 1px solid #818181; width: 20%;">
                    <b>Writeoff</b>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 98%; margin-top: 10px; margin-left: 1%;">
        <table style="width: 100%; margin-top: 10px;" class="tablecontent">
            <tr>
                <td class="headtd" style="width: 100%;" colspan="2">
                    <b>Details</b>
                </td>
            </tr>
            <tr>
                <td style="width: 55%; border-right: 1px solid #818181;" valign="top">
                    <table style="width: 100%;" cellspacing="0">
                        <tr>
                            <td style="width: 100%;" align="center" colspan="4">
                                <b>Mode of Payment</b>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:RadioButton ID="rdldd" onClick="check(this.id);" runat="server" Font-Bold="true"
                                    Text="DD/Cheque No." GroupName="mode" Checked="true" />
                            </td>
                            <td>
                                <asp:RadioButton ID="rdlcash" onClick="check(this.id);" runat="server" Font-Bold="true"
                                    Text="Cash" GroupName="mode" />
                            </td>
                            <td colspan="2">
                                <asp:RadioButton ID="rdlcard" onClick="check(this.id);" Font-Bold="true" runat="server"
                                    Text="Credit/Debit Card" GroupName="mode" />
                            </td>
                        </tr>
                        <tr>
                            <td id="tddd" runat="server">
                                <b>DD/Cheque No.</b>
                            </td>
                            <td id="tdddtext" runat="server">
                                <asp:TextBox ID="txtdd" runat="server" CssClass="inputnumber"></asp:TextBox>
                            </td>
                            <td id="tddate" runat="server">
                                <b>Date</b>
                            </td>
                            <td id="tddatetext" runat="server">
                                <asp:TextBox ID="txtmodedate" runat="server" CssClass="input" Width="70px"></asp:TextBox>
                                <%--<asp:RequiredFieldValidator ID="reqDate" runat="server" ControlToValidate="txtDate" ValidationGroup="V" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>--%>&nbsp;
                                <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/calendar.gif" />
                                <ajaxcontrol:calendarextender id="cal1" runat="server" popupbuttonid="ImageButton1"
                                    targetcontrolid="txtmodedate" format="dd/MM/yyyy">
                                </ajaxcontrol:calendarextender>
                                <ajaxcontrol:maskededitextender id="maskExt1" runat="server" masktype="Date" mask="99/99/9999"
                                    targetcontrolid="txtmodedate" culturename="pt-BR">
                                </ajaxcontrol:maskededitextender>
                                <ajaxcontrol:maskededitvalidator id="maskValid1" runat="server" controlextender="maskExt1"
                                    invalidvaluemessage="Invalid Date" isvalidempty="true" display="Dynamic" validationgroup="V"
                                    controltovalidate="txtmodedate"></ajaxcontrol:maskededitvalidator>
                            </td>
                        </tr>
                        <tr>
                            <td id="tdbank" runat="server">
                                <b>Bank/Branch</b>
                            </td>
                            <td id="tdbanktext" runat="server">
                                <asp:TextBox ID="txtBranch" runat="server" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td id="tdcard" runat="server" style="display: none;">
                                <b>Card Type</b>
                            </td>
                            <td id="tdcardtext" runat="server" style="display: none;">
                                <asp:TextBox ID="txtcardtype" runat="server" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td id="tdissued" runat="server" style="display: none;">
                                <b>Issued By</b>
                            </td>
                            <td id="tdissuedtext" runat="server" style="display: none;">
                                <asp:TextBox ID="txtissuedby" runat="server" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="width: 40%;" valign="top">
                    <table style="width: 100%;">
                        <tr>
                            <td style="width: 40%;">
                                <b>Narration</b>
                            </td>
                            <td style="width: 60%;">
                                <asp:TextBox ID="txtnarration" runat="server" Width="98%" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblcashremit" runat="server" Font-Bold="true" Text="Remitting Bank*"></asp:Label>
                            </td>
                            <td>
                                <asp:DropDownList ID="ddlCashType" runat="server" CssClass="dropdown" Width="100%"
                                    Style="display: none;">
                                    <asp:ListItem Text="--- Select Cash A/C ---" Value="0" Selected="True"></asp:ListItem>
                                </asp:DropDownList>
                                <asp:DropDownList ID="ddlBankType" runat="server" Width="100%" CssClass="dropdown">
                                    <asp:ListItem Text="--- Select Bank A/C ---" Value="0" Selected="True"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Prepared By</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPreparedby" runat="server" Width="98%" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Checked By</b>
                            </td>
                            <td>
                                <asp:TextBox ID="txtCheckedby" runat="server" Width="98%" CssClass="input"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table style="width: 90%;">
            <tr>
                <td align="center">
                    <asp:Button ID="btnSave" Text="Save" runat="server" CssClass="button" 
                        OnClientClick="return CheckBeforeSave();" onclick="btnSave_Click" />
                    &nbsp;
                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CssClass="button" />
                </td>
            </tr>
        </table>
             <asp:LinkButton ID="lnkModal" runat="server"></asp:LinkButton>
            <asp:Panel ID="panelFormatMessage" runat="server" Width="250px" Height="130px" CssClass="modalPopup" style="display:none; border:solid 1px #05DB35">
                <div style="width:100%; height:100px">
                <table width="100%" cellspacing="0" cellpadding="2">
                    <tr>
                        <td colspan="2" style="background-color:#61A737; color:White"><b>INVENTORY</b></td>
                    </tr>
                    <tr>
                        <td style="padding-top:5px">
                            <img src="../../Images/questionMark.jpg" />
                        </td>
                        <td>
                            No TDS and Writeoff Ledger<br />
                            is defined for selected format<br />
                            Do you want to define it?<br />
                        </td>
                    </tr>
                </table>
                </div>
                <div style="width:100%; height:30px">
                    <table width="100%">
                        <tr>
                            <td style="width:50%; padding-right:3px" align="right">
                                <asp:HyperLink ID="hyperTranFormat" runat="server" NavigateUrl="" CssClass="messageBox">OK</asp:HyperLink>
                            </td> 
                            <td>
                                <asp:HyperLink ID="hyperCancelFormat" runat="server" NavigateUrl="Receipt.aspx" CssClass="messageBox">CANCEL</asp:HyperLink>
                            </td>
                        </tr>
                    </table>
                </div>
            </asp:Panel>
            <AjaxControl:ModalPopupExtender ID="modalFormatExtender" runat="server" TargetControlID="lnkModal" PopupControlID="panelFormatMessage" BackgroundCssClass="modalBackground"></AjaxControl:ModalPopupExtender>         

        <asp:GridView ID="GridView2" Visible="false" runat="server">
        </asp:GridView>
        <asp:GridView ID="GridView1" Visible="false" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
